package com.backend.service.impl;

import com.backend.config.PasswordConfig;
import com.backend.entity.Result;
import com.backend.entity.ResultCode;
import com.backend.entity.dto.LoginDTO;
import com.backend.entity.dto.RepairmanRegisterDTO;
import com.backend.entity.dto.UserRegisterDTO;
import com.backend.entity.entity.Users;
import com.backend.mapper.AuthMapper;
import com.backend.service.AuthService;
import com.backend.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Objects;

/**
 * 作者:xuSen
 * 日期2025/7/20 11:04
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private AuthMapper authMapper;
    @Autowired
    private PasswordConfig passwordConfig;

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @Override
    public String registerUser(UserRegisterDTO userRegisterDTO) {
        String username = userRegisterDTO.getUsername();
        Users usersByUsername = authMapper.getUsersByUsername(username);
        if(usersByUsername != null) return "该用户名已被他人使用，请重新更换";
        String password = userRegisterDTO.getPassword();
        password=passwordConfig.passwordEncoder().encode(password);
        Users users = new Users();
//        构建用户对象
        users = users.builder()
                .username(username)
                .passwordHash(password)
                .role(0)
                .build();
        if(Objects.equals(userRegisterDTO.getInvitationCode(), "1111")){
            users.setRole(1);
        }
        authMapper.registerUser(users);
        return null;
    }

    /**
     * 修理工注册
     * @param repairmanRegisterDTO
     * @return
     */
    @Override
    public String registerRepairman(RepairmanRegisterDTO repairmanRegisterDTO) {
        if(!repairmanRegisterDTO.getInviteCode().equals(Const.REPAIRMAN_CODE)){
            return "邀请码错误";
        }
        String username = repairmanRegisterDTO.getUsername();
        Users usersByUsername = authMapper.getUsersByUsername(username);
        if(usersByUsername != null) return "该用户名已被他人使用，请重新更换";
        String password = repairmanRegisterDTO.getPassword();
        password=passwordConfig.passwordEncoder().encode(password);
        String phone = repairmanRegisterDTO.getPhone();
        Users users = new Users();
//        构建用户对象
        users = users.builder()
                .username(username)
                .passwordHash(password)
                .phone(phone)
                .role(1)
                .build();
        authMapper.registerUser(users);
        return null;
    }

    @Override
    public Users login(LoginDTO loginDTO) {

        Users user = authMapper.getUsersByUsername(loginDTO.getUsername());
        if (user == null){
//            抛出异常
            throw new RuntimeException("用户名不存在");
        }
        // 密码加密
        boolean passwordMatches = passwordConfig.passwordEncoder()
                .matches(loginDTO.getPassword(), user.getPasswordHash());
        if (!passwordMatches){
//            抛出异常
            throw new RuntimeException("密码错误");
        }
        user.setPasswordHash( null);

        return user;
    }
}
